//
//  WelcomeViewController.swift
//  01sinablog
//
//  Created by ZhangZiang on 15/12/16.
//  Copyright © 2015年 ZhangZiang. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func loadView() {
        view = backView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        // Do any additional setup after loading the view.
    }
    
    private func setupUI()
    {
        //设置头像
        view.addSubview(iconView)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(view.snp_top).offset(150)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        //设置label
        view.addSubview(tipLabel)
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(20)
            make.width.equalTo(200)
        }
        //用sdweb加载头像
        downloadIcon()
        
        //切成圆角
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        
        //此时view没frame,不行
//        showAnimate()
    }
    //此时view还没出来,不行
//    override func viewWillAppear(animated: Bool) {
//        super.viewWillAppear(animated)
//        
//        showAnimate()
//    }
    // MARK: 动画请务必在didAppear里写,否则会动画错误(注意是viewDid!)
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        showAnimate()
    }
    
    //下载头像
    private func downloadIcon()
    {
        guard let iconString = UserAccount.loadAccount()?.avatar_large else
        {
            return
        }
        if let url = NSURL(string: iconString)
        {
        iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    private func showAnimate()
    {
        tipLabel.alpha = 0

        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.iconView.snp_updateConstraints { (make) -> Void in
                make.top.equalTo(self.view.snp_top).offset(75)
            }

            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.tipLabel.alpha = 1
                    }) { (_) -> Void in
                        
//                        print("OK")
                        //这时候应该跳转界面了
                       NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewController, object: nil)
                }
                
        }
        
       
    }
  
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private lazy var iconView : UIImageView = UIImageView()
    
    //用便利函数 方便的初始化
    private lazy var tipLabel = UILabel(title: (UserAccount.loadAccount()?.name ?? "") + " 欢迎回来!", color: UIColor.blackColor(), fontSize: 15)
    
//    private lazy var tipLabel : UILabel = {
//        
//        let _tip = UILabel()
//        
//        _tip.text = (UserAccount.loadAccount()?.name ?? "") + " 欢迎回来!"//注意运算的优先级
//        
//        _tip.textColor = UIColor.blackColor()
//        _tip.font = UIFont.systemFontOfSize(15)
//        _tip.numberOfLines = 0
//        _tip.textAlignment = .Center
//        
//        return _tip
//        
//    }()
 
    private lazy var backView : UIImageView = {
       let _backView = UIImageView(image: UIImage(named: "ad_background"))

        return _backView
    }()
}

